package com.adgox.tiantianbiting.adapter;

import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.CommentBean;
import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by tang on 2019/3/20.
 */

public class CommentAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    private ArrayList<CommentBean> groupList;
    private ArrayList<ArrayList<CommentBean>> childList;

    public CommentAdapter(ArrayList<CommentBean> groupList, ArrayList<ArrayList<CommentBean>> childList) {
        this.groupList = groupList;
        this.childList = childList;
    }

    GroupViewHolder groupViewHolder;
    ChildViewHolder childViewHolder;

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (childList.get(groupPosition) == null) {
            return 0;
        } else {
            return childList.get(groupPosition).size();
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (childList.get(groupPosition) == null) {
            return null;
        } else {
            return childList.get(groupPosition).get(childPosition);
        }
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null){
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment_parent, parent, false);
            groupViewHolder.iconAvatar = convertView.findViewById(R.id.icon_avater);
            groupViewHolder.tvUsername = convertView.findViewById(R.id.tv_username);
            groupViewHolder.tvContent = convertView.findViewById(R.id.tv_content);
            groupViewHolder.tvTime = convertView.findViewById(R.id.tv_time);
            groupViewHolder.llComment = convertView.findViewById(R.id.ll_comment);
            groupViewHolder.tvCommentNum = convertView.findViewById(R.id.tv_comment_num);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        CommentBean commentBean = groupList.get(groupPosition);
        Glide.with(parent.getContext()).load(commentBean.getUser().getWxAvatarUrl()).into(groupViewHolder.iconAvatar);
        groupViewHolder.tvUsername.setText(commentBean.getUser().getWxNickname());
        groupViewHolder.tvContent.setText(commentBean.getContent());
        groupViewHolder.tvTime.setText(commentBean.getCreateTimeStr());
        groupViewHolder.tvCommentNum.setText(commentBean.getCountDiscuss()==0?"":""+commentBean.getCountDiscuss());
        groupViewHolder.llComment.setTag(groupPosition);
        groupViewHolder.llComment.setOnClickListener(this);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null){
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment_child, parent, false);
            childViewHolder.tvComment = convertView.findViewById(R.id.tv_comment);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        if (childList.get(groupPosition) == null){
            return null;
        }

        CommentBean commentBean = childList.get(groupPosition).get(childPosition);
        String s = "<font color='#ff971d'>"+commentBean.getUser().getWxNickname()+"</font>"+"："+commentBean.getContent();
        childViewHolder.tvComment.setText(Html.fromHtml(s));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public interface onGroupItemClickListener{
        void onGroupItemClick(View view,int position);
    }

    public onGroupItemClickListener listener = null;

    public void setOnGroupItemClickListener(onGroupItemClickListener listener){
        this.listener = listener;
    }

    public void addDatas(ArrayList<CommentBean> groups,ArrayList<ArrayList<CommentBean>> childs){
        groupList.addAll(groups);
        childList.addAll(childs);
        notifyDataSetChanged();
    }

    public void addSubComment(ArrayList<CommentBean> childs,int groupPosition){
        childList.set(groupPosition,childs);
        notifyDataSetChanged();
    }

    public void clear(){
        groupList.clear();
        childList.clear();
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (v.getId() == R.id.ll_comment){
            listener.onGroupItemClick(v,position);
        }
    }

    static class GroupViewHolder {
        @BindView(R.id.icon_avater)
        RoundedImageView iconAvatar;
        @BindView(R.id.tv_username)
        TextView tvUsername;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_comment_num)
        TextView tvCommentNum;
        @BindView(R.id.ll_comment)
        LinearLayout llComment;
    }

    static class ChildViewHolder {
        @BindView(R.id.tv_comment)
        TextView tvComment;
    }
}
